import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        cb = QCheckBox('Default',self)        
        cb.clicked.connect(self.OnClicked)
        layout.addWidget(cb)

        cb = QCheckBox('Three State CheckBox',self)
        cb.setTristate(True)
        cb.stateChanged.connect(self.OnStateChanged)
        layout.addWidget(cb)

        cb = QCheckBox('CheckBox with icon',self)
        cb.setIcon(QIcon(f'{self.parent.getAppDir()}/resources/pyside_icon.ico'))
        cb.clicked.connect(self.OnClicked)
        layout.addWidget(cb)

        groupBox = QGroupBox('Exclusive CheckBoxes')        
        self.groupExclusive = QButtonGroup()
        self.groupExclusive.setExclusive(True)        
        groupLayout = QVBoxLayout()

        cb = QCheckBox('CheckBox1',self,objectName='CheckBox1')        
        #cb.clicked.connect(self.OnGroupClicked)
        groupLayout.addWidget(cb)
        self.groupExclusive.addButton(cb)

        cb = QCheckBox('CheckBox2',self,objectName='CheckBox2')        
        #cb.clicked.connect(self.OnGroupClicked)
        groupLayout.addWidget(cb)
        self.groupExclusive.addButton(cb)

        cb = QCheckBox('CheckBox3',self,objectName='CheckBox3')        
        #cb.clicked.connect(self.OnGroupClicked)
        groupLayout.addWidget(cb)
        self.groupExclusive.addButton(cb)
        self.groupExclusive.buttonClicked.connect(self.OnGroupClicked)

        groupBox.setLayout(groupLayout)
        layout.addWidget(groupBox)        

        groupBox = QGroupBox('NonExclusive CheckBoxes')        
        self.groupNonExclusive = QButtonGroup()  
        self.groupNonExclusive.setExclusive(False)      
        groupLayout = QVBoxLayout()

        cb = QCheckBox('CheckBox4',self,objectName='CheckBox4')        
        #cb.clicked.connect(self.OnNoGroupClicked)
        groupLayout.addWidget(cb)
        self.groupNonExclusive.addButton(cb)

        cb = QCheckBox('CheckBox5',self,objectName='CheckBox5')        
        #cb.clicked.connect(self.OnNoGroupClicked)
        groupLayout.addWidget(cb)
        self.groupNonExclusive.addButton(cb)

        cb = QCheckBox('CheckBox6',self,objectName='CheckBox6')        
        #cb.clicked.connect(self.OnNoGroupClicked)
        groupLayout.addWidget(cb)
        self.groupNonExclusive.addButton(cb)
        self.groupNonExclusive.buttonClicked.connect(self.OnNoGroupClicked)

        groupBox.setLayout(groupLayout)
        layout.addWidget(groupBox)  

        self.setLayout(layout)

    def OnClicked(self,event):
        self.parent.OnShowMessage(f'CheckBox Clicked,isChecked:{event}')
    def OnStateChanged(self,state):
        self.parent.OnShowMessage(f'CheckBox State:{state}')
    def OnGroupClicked(self,event):
        self.parent.OnShowMessage(f'Exclusive {self.groupExclusive.checkedButton().text()} Clicked')
        #objectName = self.sender().objectName()
        #self.parent.OnShowMessage(f'Exclusive {objectName} Clicked,isChecked:{event}')
    def OnNoGroupClicked(self,event):
        self.parent.OnShowMessage(f'NonExclusive {self.groupNonExclusive.checkedButton().text()} Clicked,isChecked:{self.groupNonExclusive.checkedButton().isChecked()}')
        #objectName = self.sender().objectName()
        #self.parent.OnShowMessage(f'Non Exclusive {objectName} Clicked,isChecked:{event}')
    
        

def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet